﻿using Commen;
using DAL;
using DbEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AyaWo.SpiderService
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //var tyt = new main();
            //Application.Run(tyt);

            string user = Caches.Read<string>("userInfo");

            if (user.IsNullOrWhiteSpace())
            {
                UserInfo ui = DatHelper<UserInfo>.GetDat("userInfo.dat");

                if (ui != null)
                {
                    if (ui.IsAuto == true)
                    {
                        bool logisok = UserInfoService.Loging(ui);
                        if (logisok)
                        {
                            Caches.Write<string>("userInfo", ui.userName);
                            TODO();
                        }
                    }
                    else
                    {

                        LogForm cf = new LogForm();
                        if (cf.ShowDialog() == DialogResult.OK)
                        {
                            TODO();
                        }
                        else
                        {
                            return;
                        }
                    }
                }
                else
                {

                    LogForm cf = new LogForm();
                    if (cf.ShowDialog() == DialogResult.OK)
                    {
                        TODO();
                    }
                    else
                    {
                        return;
                    }
                }
            }
            else
            {
                TODO();
            }
        }

      static void TODO()
        {
            var va = Commen.DatHelper<string>.GetDatStr("connStr.dat");
            try
            {
                DbF.DbHelper dh = new DbF.DbHelper(va, DbF.SqlSourceType.MySql);
                dh.OpenConnection();
                dh.Dispose();
                 Application.Run(new main());
            }
            catch
            {
                ConnForm cf = new ConnForm();
                if (cf.ShowDialog() == DialogResult.OK)
                {
                    Application.Run(new main());
                }
                else
                {
                    return;
                }
            }
        }
    }
}
